$package("dowry.util");

$import("$");
$import("dowry.util.EventUtils");
$import("dowry.test.Test");

$class("EventUtilsTest").$extends("Test").$as(
{
    setup : function()
    {
        this.event = new dowry.util.EventUtils();
        this.called = false;

        var sel = document.createElement("select");
        sel.setAttribute("id", "select");
        document.body.appendChild(sel);

        var textinput = document.createElement("input");
        textinput.setAttribute("id", "textinput");
        textinput.setAttribute("type", "text");
        document.body.appendChild(textinput);

        var radio = document.createElement("input");
        radio.setAttribute("id", "radio");
        radio.setAttribute("type", "radio");
        document.body.appendChild(radio);
    },

    testAddEvent : function()
    {
        // not sure what else we can test without
        // some way of automating a browser event
        // firing... the
        this.assert(this.event.addEvent);
    },

    testDispatchEvent : function()
    {
        this.assert(this.event.dispatchEvent);

        this.assertEqual(this.called, false);
        this.event.addEvent("select", "change", this.eventListener);
        this.event.dispatchEvent("select", "change");
        this.assertEqual(this.called, true);
    },

    testDispatchEvent2 : function()
    {
        this.assertEqual(this.called, false);
        this.event.addEvent("textinput", "change", this.eventListener);
        this.event.dispatchEvent("textinput", "change");
        this.assertEqual(this.called, true);
    },

    testDispatchEvent3 : function()
    {
        this.assertEqual(this.called, false);
        this.event.addEvent("textinput", "keypress", this.eventListener);
        this.event.dispatchEvent("textinput", "keypress");
        this.assertEqual(this.called, true);
    },

    testDispatchEvent4 : function()
    {
        this.assertEqual(this.called, false);
        this.event.addEvent("radio", "click", this.eventListener);
        this.event.dispatchEvent("radio", "click");
        this.assertEqual(this.called, true);
    },

    testRemoveEvent1 : function()
    {
        this.assertEqual(this.called, false);
        this.event.addEvent("textinput", "change", this.eventListener);
        this.event.removeEvent("textinput", "change", this.eventListener);
        this.event.dispatchEvent("textinput", "change");
        this.assertEqual(this.called, false);
    },

    testRemoveEvent2 : function()
    {
        this.assertEqual(this.called, false);
        this.event.addEvent("textinput", "change", this.eventListener);
        this.event.addEvent("textinput", "change", this.eventListener2);
        this.event.removeEvent("textinput", "change", this.eventListener);
        this.event.dispatchEvent("textinput", "change");
        this.assertEqual(this.called, true);
    },

    eventListener : function()
    {
        this.called = true;
    },

    eventListener2 : function()
    {
        this.called = true;
    },

    teardown : function()
    {
        this.event = null;
        this.called = false;

        document.body.removeChild($("select"));
        document.body.removeChild($("textinput"));
        document.body.removeChild($("radio"));
    }
});